package net.galluzzo.wave.orthopermubot.permutation;

import java.util.Random;

/**
 * A permutation that only fires some of the time, and returns the identity
 * transform at other times.
 * 
 * @author Eric Galluzzo
 */
public class OccasionalPermutation implements Permutation {
	protected Random random = new Random();
	protected Permutation delegate;
	protected double probability;
	
	/**
	 * Creates an <code>OccasionalPermutation</code> that calls the given
	 * delegate {@link Permutation} with probability <code>probability</code>.
	 * 
	 * @param delegate     The delegate {@link Permutation}
	 * @param probability  The probability of calling the <code>delegate</code>
	 */
	public OccasionalPermutation( Permutation delegate, double probability ) {
		if ( delegate == null ) {
			throw new IllegalArgumentException( "delegate cannot be null" );
		}
		if ( probability < 0.0 || probability > 1.0 ) {
			throw new IllegalArgumentException( "probability must be between 0.0 and 1.0" );
		}
		
		this.delegate = delegate;
		this.probability = probability;
	}
	
	public String permute( String str ) {
		if ( random.nextDouble() < probability ) {
			return delegate.permute( str );
		}
		return str;
	}
}
